<?php
class Categories extends MX_Controller
{
    function __construct()
    {
        parent::__construct();
//        $this->output->enable_profiler(TRUE);
    }

    function get_left_categories($options = array())
    {   
        $cond = array();
        
        if(isset($options['lang']))             $cond['lang']   = $options['lang'];
        if(isset($options['product_cat_id']))   $cond['cat_id'] = $options['product_cat_id'];
        if(!isset($cond['cat_id'])) $cond['cat_id'] = 0;
        $cond['categories']  = $this->categories_model->get_categories($cond);
        $cond['parent_id']   = ROOT_CATEGORY_ID;
        $categories_menu    = '<ul class="nav nav-list side_bar">';
        $categories_menu   .= $this->_visit_cat($cond);
        $categories_menu   .= '</ul>';
        $view_data = array();
        $view_data['categories_menu'] = $categories_menu;

        return $this->load->view('products/categories/categories_menus', $view_data, TRUE);
    }


    private function _visit_cat($params = array())
    {
        $output                 = '';
        $sub_cats               = $this->_get_sub_cats($params);
        foreach($sub_cats as $cat)
        {
            if ($cat->id == $params['cat_id']) $selected = ' class="active"'; else $selected = '';
            $current_cat = $this->categories_model->get_categories(array('id' => $params['cat_id']));
            if(count($current_cat) > 0) 
            {
                $current_lv = $current_cat[0]->level;
                $current_parent_id = $current_cat[0]->parent_id;
            }
            if($cat->level == 1)
                $output             .= '<li class="nav-header s_top_a">' . $cat->category . '</li>';
            
            else if($cat->level == 2)
                
                $output             .= '<li' . $selected . '><a href="'. get_base_url() . url_title($cat->category, 'dash', TRUE) . '-c' . $cat->id .  '">
                                        ' . $cat->category . '</a>';
            else if(isset($current_lv) && ($current_lv == 2 && $cat->parent_id == $params['cat_id']))
            {
                $output             .= '<li' . $selected . '><a href="' . get_base_url() . url_title($cat->category, 'dash', TRUE) . '-c' . $cat->id .  '">
                                        ' . $cat->category . '</a>';
                    
            }
            else if(isset($current_lv) && ($current_lv == 3 && $cat->parent_id == $current_parent_id))
            {
                $output             .= '<li' . $selected . '><a href="' . get_base_url() . url_title($cat->category, 'dash', TRUE) . '-c' . $cat->id .  '">
                                        ' . $cat->category . '</a>';
            }
            
            $params['parent_id']= $cat->id;
            if($cat->level != 1 && $cat->level != 3)
                $output             .= '<ul class="st_li">';
            $output             .= $this->_visit_cat($params);
            if($cat->level != 1 && $cat->level != 3)
                $output             .= '</ul></li>';
        }

        return $output;
    }

    private function _get_sub_cats($params = array())
    {
        $cats      = $params['categories'];
        $sub_cats  = array();

        foreach($cats as $index => $menu)
        {
            if ($menu->parent_id == $params['parent_id'])
                $sub_cats[$index] = $menu;
        }
        return $sub_cats;
    }

    /**
     * Trả lại combo box bao gồm các danh mục sản phẩm
     *
     * @author Tuấn Anh
     * @date   2011-05-02
     * @param type $options
     */
    function get_categories_combo($options = array())
    {
        return $this->categories_model->get_categories_combo($options);
    }
    
    function get_top_categories($options = array())
    {
//        echo '<pre>';
//        print_r($options);
//        echo '</pre>';
//        die;
        $this->output->javascripts("run_slide_top_categories('#top_categories');");
        if(!isset($options['product_cat_id']))
        {
            $categories = $this->categories_model->get_categories(array('parent_id' => ROOT_CATEGORY_ID));
        }
        else
        {
            $check_product              = $this->products_model->get_products(array('my_cat_id' => $options['product_cat_id']));
            if(count($check_product) > 0)
            {
                $categories = $this->categories_model->get_categories(array('parent_id' => $check_product[0]->parent_id));
            }
            else
                $categories = $this->categories_model->get_categories(array('parent_id' => $options['product_cat_id']));
        }
        $view_data = array();
        $view_data['categories'] = $categories;
        $view_data['product_cat_id'] = $options['product_cat_id'];
        return $this->load->view('/categories/top_categories', $view_data, TRUE);
    }
    
    function get_categories_hp()
    {
        $options                = array();
        $options['parent_id']   = ROOT_CATEGORY_ID;
        $view_data              = array();
        $view_data['categories']= $this->categories_model->get_categories($options);
        return $this->load->view('categories/categories_hp', $view_data, TRUE);
    }
}
?>
